package com.oocl.o2o.merchant.controller.rest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.oocl.o2o.merchant.entity.Food;
import com.oocl.o2o.merchant.entity.FoodType;
import com.oocl.o2o.merchant.entity.Merchant;
import com.oocl.o2o.merchant.service.FoodManager;

import static com.oocl.o2o.merchant.util.ImageUtil.*;

@RestController
@RequestMapping("/sec/rest/food")
public class FoodController {
	@Autowired
	private FoodManager foodManager;

	@GetMapping("/{id}")
	public Food getFood(@PathVariable("id") String id) {
		if (id.length() != 32) {
			return null;
		}
		Food food = foodManager.findById(id);
		return food;
	}

	@GetMapping
	public List<Food> getMyFood(@SessionAttribute String merchantId) {
		return foodManager.findByMerchantId(merchantId);
	}
	

	@PostMapping
	public boolean addFood(String name, Double price, MultipartFile image, String foodTypeId,
			@SessionAttribute String merchantId) {
		File imageFile = null;
		try {
			String imagePath = "/food/" + UUID.randomUUID().toString() + getSuffix(image.getOriginalFilename());
			Merchant merchant = new Merchant(merchantId, null, null, null, null, null);
			FoodType foodType = new FoodType(foodTypeId, null);
			Food food = new Food(null, name, price, imagePath, merchant, foodType);
			// transfer to file
			imageFile = new File(getAbsolutePath(imagePath));
			image.transferTo(imageFile);
			food = foodManager.add(food);
			if (food == null) {
				imageFile.delete();
				return false;
			}
			return true;
		} catch (IllegalStateException | IOException e) {
			imageFile.delete();
			e.printStackTrace();
			return false;
		}

	}

	@PostMapping("/update.action")
	public boolean updateFood(String id, String name, Double price, MultipartFile image, String foodTypeId,
			@SessionAttribute String merchantId) {
		File imageFile = null;
		try {
			String imagePath = "/food/" + UUID.randomUUID().toString() + getSuffix(image.getOriginalFilename());
			Merchant merchant = new Merchant(merchantId, null, null, null, null, null);
			FoodType foodType = new FoodType(foodTypeId, null);
			Food food = new Food(id, name, price, imagePath, merchant, foodType);
			// transfer to file
			imageFile = new File(getAbsolutePath(imagePath));
			image.transferTo(imageFile);
			food = foodManager.update(food);
			if (food == null) {
				imageFile.delete();
				return false;
			}
			return true;
		} catch (IllegalStateException | IOException e) {
			imageFile.delete();
			e.printStackTrace();
			return false;
		}
	}

	@DeleteMapping("/{id}")
	public boolean deleteFood(@PathVariable("id") String id) {
		Food food = foodManager.delete(id);
		if (food == null) {
			return false;
		}
		return true;
	}
}
